package com.ilioili.appframe.waterfalldemo;

import java.io.File;
import java.util.ArrayList;
import java.util.Random;

import util.bitmap.FileUtil;
import util.bitmap.Img;
import util.bitmap.ImgPool;
import util.bitmap.ImgView;
import util.bitmap.TestPictures;

import com.ilioili.waterfall.Adapter;
import com.ilioili.waterfall.WaterFall;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

public class WaterfallDemo extends Activity {
	private ArrayList<Img<ItemData>> dataList = new ArrayList<Img<ItemData>>();
	private ImgPool imgPool = new ImgPool() {
		@Override
		public void bitmapDecoded(Img<?> img, View v) {
			ImageView iv = (ImageView) v;
			iv.setImageBitmap(img.getBitmap());
//			v.startAnimation(getAnimation());
			v.startAnimation(getAnimation());
		}
//		public Animation getAlphaAnimation(){
//			Animation a = new AlphaAnimation(0f, 1);
//			a.setDuration(500);
//			return a;
//		}
		public Animation getAnimation(){
			Animation aa = new AlphaAnimation(0f, 1f);
			aa.setDuration(1000);
			return aa;
		}
		@Override
		public void bitmapNotDecoded(View v) {
			ImageView iv = (ImageView) v;
			iv.setImageBitmap(null);
		}
	};
	private int column = 2;
	private WaterFall waterFall;
	private Adapter adapter = new Adapter() {
		
		@Override
		public View getView(View v, ViewGroup vp, int position) {
			Img<ItemData> img = dataList.get(position);
			ItemData item = img.getData();
			ImgView iv = (ImgView) v;
			if(null==iv){
				iv = new ImgView(WaterfallDemo.this);
				iv.setPadding(5, 5, 5, 5);
			}
			float radio = item.height/(float)item.width;
			iv.setRatioBaseOnWidth(radio);
			imgPool.bind(img, iv);
			return iv;
		}
		
		@Override
		public int getCount() {
			return dataList.size();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FileUtil.init(getApplication());//Img类需要
		waterFall = new WaterFall(this);
		waterFall.setColumn(column);
		waterFall.setAdapter(adapter);
		setContentView(waterFall);
		getTestDatas();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		imgPool.shutdown();
	}
	
	
	/**
	 * 一般情况下，服务器返回的图片数据应该包含图片的URL，Width，Height，这样子才可以在图片未下载之前就排好位置。
	 */
	public void getTestDatas(){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds=true;
		int width = getResources().getDisplayMetrics().widthPixels/column;
		Random r = new Random(0);
		File fileDir = FileUtil.getFileDir();
		File cacheDir = FileUtil.getCacheDir();
		for(int i = 0; i<TestPictures.URLS.length; i++){
			String url = TestPictures.URLS[i];
			ItemData item = new ItemData();
			Img<ItemData> img = new Img<ItemData>(cacheDir, fileDir, url);
			item.url = url;
			item.width = width;
			if(img.fileExists()){
				BitmapFactory.decodeFile(img.getImagePath(), options);
				item.height = item.width*options.outHeight/options.outWidth;
			}else{
				item.height = (int) (width*(5+r.nextInt(10))/10f);
			}
			img.setData(item);
			img.setSizeBaseOnWidth(item.width);
			dataList.add(img);
		}
	}
	
}
